-- file: myTest/bisec.hs

-- 类型名要大写
-- 求根程序 参数
data Paras = Paras
  { pStt :: Double,
    pSect :: Double,
    pEnd :: Double,
    px0 :: Double,
    px1 :: Double,
    px2 :: Double,
    pSecTol :: Double,
    pSolTol :: Double,
    pLoops :: Int
  }
  deriving (Show)

-- 求根区间参数
data MyRng = MyRng
  { iA :: Double,
    iB :: Double
  }
  deriving (Show)

-- 判断是否收敛
myConverge :: (Ord a, Num a) => a -> (a, a) -> Bool
myConverge c (a, b)
  | abs (a - b) < c = True
  | otherwise = False

-- 迭代函数, 区间二分
myIter :: (Ord a, Fractional b, Num a) => a -> (b -> a) -> (b, b) -> (b, b)
myIter tol f (a, b)
  | abs (f c) < tol = (c, c)
  | (f a) * (f c) <= 0 = (a, c)
  | otherwise = (c, b)
  where
    c = (a + b) / 2.0

-- 容差
tol :: Double
tol = 1.0E-18

-- 测试例子
oRng :: (Double, Double)
oRng = until (myConverge tol) (myIter tol sin) (-0.9, 1.2)

-- 中点
oAns = sum oRng / 2.0